<template>
	<view class="main">
		<view class="pic">
			<image :src="avatar"></image>
		</view>
		<view class="pic_btn">
			<view @click="resetavatar" class="reset">重选</view>
			<view @click="uploadavatar" class="upload">确认</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				avatar:false,
			}
		},
		methods: {
			resetavatar(){
				uni.chooseImage({
				    count: 1,
				    sizeType: ['original', 'compressed'],
				    sourceType: ['album','camera'],
				    success: (res)=>{
						var tempfile= res.tempFilePaths[0];
				        this.avatar = tempfile;
				    }
				});
			},
			uploadavatar(){
				this.api.upload({
					url:'/api/v1/user/avatar',
					name:'avatar',
					file:this.avatar
				}).then(res=>{
					res = JSON.parse(res);
					if(res.code=='701'){
						this.api.setuser('avatar',res.data);
					}
					uni.showToast({
						title:res.msg,
						duration: 1000,
						icon: "none"
					});
				}).catch(err=>{
				});
			}
		},
		onLoad(params){
			console.log('avatar_onLoad');
			this.avatar = params.tempfile;
		}
	}
</script>

<style>
.pic{width: 100%;position: fixed;top: 88rpx; left: 0;bottom: 100rpx;}
.pic_btn{width: 90%;margin:0 5%;height: 100rpx;position: fixed; bottom: 0;display: flex;justify-content: space-between;}
.pic_btn .reset{width: 140rpx;margin:15rpx 0;height: 70rpx;
	line-height: 70rpx;}
.pic_btn .upload{width:140rpx;margin:15rpx 0;
    height: 70rpx;
	line-height: 70rpx;
    color: #fff;
    background: #06b303;
    text-align: center;}
.pic image{width: 100%;height: 100%;}
</style>
